$(document).ready(function(){
	var base_url = "http://" + window.location.hostname + "/";
	var teacher_verify_path = base_url + 'index.php/backend/teacher/verify/';
	
	$.ajaxSetup({
		type: "POST",
		cache: false
	});
	
	/*----------------------------------------
	Backend Society Verify
	----------------------------------------*/
	$('#society_verify_dialog').dialog({
		autoOpen: false,
		resizable: false,
		draggable: false,
		modal: true,
		width: 900,
		height: 630
	});
	
	/* 社团详细信息 */
	$(".teacher_verify_manage").live("click", function(){
		$("#society_verify_dialog").dialog("open");
		$("#society_verify_dialog").spin();
		$.ajax({
			url: teacher_verify_path + "ajax_get_so_info",
			dataType: 'json',
			success: function(data) {
				$("#society_verify_dialog").spin(false);
				if (data['society'].confirm_FLAG == "2;0;0") {
					var society_name = data['society']['s_name'];
					$("#society_verify_dialog").dialog({
						buttons: {
							"审核日志": function() {
								ajax_get_society_log();
							},
							"审核通过": function() {
								ajax_society_verify_pass();
							},
							"审核失败": function() { 
								ajax_society_verify_failed();
							},
							"取消": function() {
								$("#society_verify_dialog").dialog("close");
							}
						}
					});
				} else {
					$("#society_verify_dialog").dialog({
						buttons: {
							"审核日志": function() {
								ajax_get_society_log();
							},
							"取消": function() {
								$("#society_verify_dialog").dialog("close");
							}
						}
					});
				}
				open_society_verify_dialog(data);
			}
		});
	});
	
	function open_society_verify_dialog(data) {
		
		/*往社团基本信息栏及社团负责人信息栏中加内容*/
		document.getElementById("ui-dialog-title-society_verify_dialog").innerHTML = data['society']['s_name'] + "(" + getSocietyStatusByFlag(data['society']['confirm_FLAG']) + ") - 复审资料";
		for (x in data['society']) {
			if (document.getElementById(x)) {
				if (data['society'][x] != null) {
					if (x != "contact_col" && x != "contact_pro" && x != "s_category") {
						document.getElementById(x).innerHTML = data['society'][x];
					} else if (x == "s_category") {
						document.getElementById("s_category").innerHTML = getSocietyCategoryNameById(data['society']['s_category']);
					}
				} else {
					document.getElementById(x).innerHTML = "尚未填写";
				}
			}
		}
		if (data['society']['contact_col'] != null && data['society']['contact_pro'] != null) {
			document.getElementById("col_pro").innerHTML = getColProNameById(data['society']['contact_col']) + " - " + getColProNameById(data['society']['contact_pro']);
			$("#col_pro").attr("title", getColProNameById(data['society']['contact_col']) + " - " + getColProNameById(data['society']['contact_pro']));
			//<!-- 截取字符串 -->
			$("#col_pro").each(function(){
				if ($(this).text().length > 22) {
					$(this).text($(this).text().substring(0, 22) + "...");
				}
			});
		} else {
			document.getElementById("col_pro").innerHTML = "尚未填写";
		}
		
		/*往社团指导老师信息栏中加内容*/
		for (x in data['teacher']) {
			if (document.getElementById(x)) {
				if (data['teacher'][x] != null) {
					if (x != "t_birth") {
						document.getElementById(x).innerHTML = data['teacher'][x];
					} else {
						document.getElementById(x).innerHTML = convert_birthday(data['teacher'][x]);
					}
				} else {
					document.getElementById(x).innerHTML = "尚未填写";
				}
			}
		}
	}
	
	function ajax_society_verify_pass() {
		$(".confirm_dialog").remove();
		$("#content").append(
			"<div class='confirm_dialog' title='确认你的审核结果?'>" +
				"<form>" +
				"<p><span class='ui-icon ui-icon-alert' style='float:left; margin:9px 5px 5px 0;'></span>该社团状态将被设置为<spans>复审通过</spans>，你确定吗？若选是，请填写备注</p>" +
				"<textarea id='remark' style='width:320px; height:120px' class='text ui-widget-content ui-corner-all'></textarea>" +
				"</form>" +
			"</div>"
		);
		
		$(".confirm_dialog").dialog({
			resizable: false,
			draggable: false,
			modal: true,
			width: 373,
			height: 330,
			buttons: {
				"是": function() {
					var remark = document.getElementById("remark").value;
					$.ajax({
						url: teacher_verify_path + 'ajax_verify_pass',
						dataType: 'json',
						data: {remark: remark},
						success: function() {
							window.location.reload();
						}
					});
				},
				"否": function() {
					$(this).dialog( "close" );
				}
			}
		});
	}
	
	function ajax_society_verify_failed() {
		$(".confirm_dialog").remove();
		$("#content").append(
			"<div class='confirm_dialog' title='确认你的审核结果?'>" +
				"<form>" +
				"<p><span class='ui-icon ui-icon-alert' style='float:left; margin:9px 5px 5px 0;'></span>该社团状态将被设置为<spans>复审失败</spans>，你确定吗？若选是，请填写备注</p>" +
				"<textarea id='remark' style='width:320px; height:120px' class='text ui-widget-content ui-corner-all'></textarea>" +
				"</form>" +
			"</div>"
		);
		$(".confirm_dialog").dialog({
			resizable: false,
			draggable: false,
			modal: true,
			width: 373,
			height: 330,
			buttons: {
				"是": function() {
					var remark = document.getElementById("remark").value;
					$.ajax({
						url: teacher_verify_path + 'ajax_verify_failed',
						dataType: 'json',
						data: {remark: remark},
						success: function() {
							window.location.reload();
						}
					});
				},
				"否": function() {
					$(this).dialog( "close" );
				}
			}
		});
	}
	
	$('#society_verify_log_dialog').dialog({
		autoOpen: false,
		resizable: false,
		draggable: false,
		modal: true,
		width: 840,
		height: 500,
		buttons: {
			"确定": function() {
				$(this).dialog("close");
			}
		}
	});
	
	/* 社团审核日志 */
	function ajax_get_society_log(){
		$("#society_verify_log_dialog").dialog("open");
		$("#society_verify_log_dialog").spin();
		$.ajax({
			url: teacher_verify_path + "ajax_get_so_log",
			dataType: 'json',
			success: function(data) {
				$("#society_verify_log_dialog").spin(false);
				open_society_verify_log_dialog(data);
			}
		});
	};
	
	function open_society_verify_log_dialog(data) {
		$("#society_verify_log_dialog").empty();
		/*往社团审核日志对话框中加内容*/
		document.getElementById("ui-dialog-title-society_verify_log_dialog").innerHTML = data['s_name'] + " - 审核日志";
		if (data['signin_log'] == "") {
			$("#society_verify_log_dialog").append("<spans>该社团暂时没有审核日志</spans>");
			return;
		}
		
		$("#society_verify_log_dialog").append(
			"<table id='log_table' class='full'>" +
			"<thead>" +
			"<tr>" +
				"<th>执行人</th>" +
				"<th style='text-align:center;' >执行人角色</th>" +
				"<th style='text-align:center;' >社团状态</th>" +
				"<th style='text-align:center;' >执行时间</th>" +
				"<th style='text-align:center;' >备注</th>" +
			"</tr>" +
			"</thead>" +
			"<tbody>" +
			"</tbody>" +
            "</table>"
		);
		for (var i = data['signin_log'].length - 1; i >= 0; i--) {
			$("#log_table tbody").append(
				"<tr>" +
                "<td style='text-align:center; width:80px;' >" + data['signin_log'][i]['execute_user'] + "</td>" +
                "<td style='text-align:center; width:80px;' >" + data['signin_log'][i]['execute_role'] + "</td>" +
                "<td style='text-align:center; width:70px;' >" + data['signin_log'][i]['status'] + "</td>" +
                "<td style='text-align:center; width:70px;' >" + data['signin_log'][i]['time'] + "</td>" +
                "<td><spans>" + data['signin_log'][i]['remark'] + "</spans></td>" +
                "</tr>"
			);
		}
	}
});